package com.bwie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.mapper.ProdMapper;
import com.bwie.pojo.Prod;
import com.bwie.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.ProdAttaMapper;
import com.bwie.pojo.ProdAtta;
import com.bwie.service.ProdAttaService;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProdAttaServiceImpl extends ServiceImpl<ProdAttaMapper, ProdAtta> implements ProdAttaService{
    @Autowired
    private ProdAttaMapper prodAttaMapper;
    @Autowired
    private ProdMapper prodMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addProdAtta(ProdAtta prodAtta) {
        Prod prodAtta1 = prodMapper.selectById(prodAtta.getProdId());
        if(ObjectUtils.isEmpty(prodAtta1)){
            return R.ERROR(500,"商品不存在");
        }
        prodAttaMapper.delete(
                new QueryWrapper<ProdAtta>().lambda().eq(ProdAtta::getProdId,prodAtta.getProdId())
        );
        prodAttaMapper.insert(prodAtta);
        return R.OK();
    }
}
